package com.rp.article.html.controller;

import com.mongodb.client.gridfs.GridFSBucket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpStatus;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

@RestController
@Api(value = "静态化文章业务controller", tags = {"静态化文章业务controller"})
@RequestMapping("/article/html")
public class ArticleHTMLController {

    @Autowired
    private GridFSBucket gridFSBucket;

    @Value("${freemarker.html.target}")
    private String htmlTarget;

    @GetMapping("/download")
    @ApiOperation(value = "下载html", notes = "下载html", httpMethod = "GET")
    public Integer download(String articleId, String articleMongoId) {

        try {
            htmlTarget = htmlTarget + File.separator + articleId + ".html";
            File file = new File(htmlTarget);
            OutputStream fileOutputStream = new FileOutputStream(file);
            gridFSBucket.downloadToStream(new ObjectId(articleMongoId), fileOutputStream);
        } catch (Exception e) {

        }

        return HttpStatus.SC_OK;
    }
}
